 

package com.zdkj.subway.stm.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.entity.SwStOtherFare;
import com.zdkj.subway.stm.service.SwStOtherFareService;
import com.zdkj.subway.stm.util.BeanPropertyUtils;
import com.zdkj.subway.stm.util.ObjectValueUtils;
import com.zdkj.subway.stm.util.SelMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 其他票款管理
 *
 * @author zdkj code generator
 * @date 2020-05-25 10:42:55
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstotherfare" )
@Api(value = "swstotherfare", tags = "其他票款管理管理")
public class SwStOtherFareController {

    private final  SwStOtherFareService swStOtherFareService;
    /**
     * 分页查询
     * @param page 分页对象
     * @param swStOtherFare 其他票款管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/pageBack" )
    public R getSwStOtherFarePageLink(Page page, SwStOtherFare swStOtherFare) {

        return R.ok(swStOtherFareService.pageLink(page, Wrappers.query(swStOtherFare)
                .allEq(SelMapUtils.getSwStOthreFareMap(swStOtherFare),false)
                .eq("a.LINE_CODE",swStOtherFare.getLineCode())
                .eq("a.STATION_CODE",swStOtherFare.getStationCode())
        ));
    }


    /**
     * 分页查询
     * @param page 分页对象
     * @param swStOtherFare 其他票款管理
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getSwStOtherFarePage(Page page, SwStOtherFare swStOtherFare) {
        if (SecurityUtils.getUser().getBelongToLine()) {
            return R.ok(swStOtherFareService.page(page, Wrappers.query(swStOtherFare)
            ));
        } else {
            QueryWrapper<SwStOtherFare> queryWrapper = Wrappers.query(swStOtherFare);
            queryWrapper.lambda().eq(SwStOtherFare::getLineCode, SecurityUtils.getUser().getLineCode());
            queryWrapper.lambda().eq(SwStOtherFare::getStationCode, SecurityUtils.getUser().getStationCode());
            return R.ok(swStOtherFareService.page(page,queryWrapper)
            );
        }
    }


    /**
     * 通过id查询其他票款管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(swStOtherFareService.getById(id));
    }

    /**
     * 新增其他票款管理
     * @param swStOtherFare 其他票款管理
     * @return R
     */
    @ApiOperation(value = "新增其他票款管理", notes = "新增其他票款管理")
    @SysLog("新增其他票款管理" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('stm_swstotherfare_add')" )
    public R save(@RequestBody SwStOtherFare swStOtherFare) {
        SwStOtherFare defaultObj = ObjectValueUtils.getDefaultObj(SwStOtherFare.class);
        BeanUtil.copyProperties(swStOtherFare, defaultObj, CopyOptions.create()
                .setIgnoreProperties(BeanPropertyUtils.getNullAndEmptyPropertyNames(swStOtherFare)));
        defaultObj.setLineCode(SecurityUtils.getUser().getLineCode());
        defaultObj.setStationCode(SecurityUtils.getUser().getStationCode());
        return R.ok(swStOtherFareService.save(defaultObj));
    }

    /**
     * 修改其他票款管理
     * @param swStOtherFare 其他票款管理
     * @return R
     */
    @ApiOperation(value = "修改其他票款管理", notes = "修改其他票款管理")
    @SysLog("修改其他票款管理" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('stm_swstotherfare_edit')" )
    public R updateById(@RequestBody SwStOtherFare swStOtherFare) {
        return R.ok(swStOtherFareService.updateById(swStOtherFare));
    }

    /**
     * 通过id删除其他票款管理
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除其他票款管理", notes = "通过id删除其他票款管理")
    @SysLog("通过id删除其他票款管理" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('stm_swstotherfare_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(swStOtherFareService.removeById(id));
    }

    /**
     * 通过id批量删除
     * @param idList id
     * @return R
     */
    @ApiOperation(value = "通过id批量删除", notes = "通过id批量删除")
    @SysLog("通过id批量删除")
    @DeleteMapping("/delBatch")
    @Transactional
    @PreAuthorize("@pms.hasPermission('stm_swstotherfaret_delBatch')")
    public R batchRemoveById(@RequestBody List<Integer> idList) {
        swStOtherFareService.removeByIds(idList);
        return R.ok();
    }

}
